<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 意见反馈
 * @author heyu
 */
class Feedback extends MY_Controller {

	/**
	 * @var DlnFeedbackModel
	 */
	public $mFeedback = null;
	
	/**
	 * @var DlnUserModel
	 */
	public $mMUser = null;
	
	public function __construct(){
		parent::__construct();
        $this->loadModel('DlnFeedbackModel', 'mFeedback', 'deliveryman');
        $this->loadModel('DlnUserModel', 'mMUser', 'deliveryman');
	}
	
	/**
	 * 首页
	 */
	public function index(){
        $data = array(
            'title' => '意见反馈',
        	'status' => $this->mFeedback->status()
        );
		$this->view('deliveryman/feedback/home', $data, 'datepicker,datatable,validation,datepicker');
	}
	
	/**
	 * 获取列表
	 */
	public function getList(){
		
		$content = trim(get('content'));
		$status = (int)get('status');
		$dateFrom = trim(get('send_date_from'));
		$dateTo = trim(get('send_date_to'));
		$iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
		$sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"
		
		if ($content){
			$this->db->like('content', $content);
		}
		
		if ($status){
			$this->db->where(array('status' => $status));
		}
		
		if($dateFrom){
			$this->db->where('f.addtime >=', strtotime($dateFrom));
		}
		
		if($dateTo){
			$this->db->where('f.addtime <', strtotime('+1day', strtotime($dateTo)));
		}
		
		$this->db->select('f.id,u.name,f.content,f.reply,f.status,f.addtime')
				 ->join($this->mMUser->tbUser.' u', 'u.id = f.deliveryman_id')
				 ->where_in('u.county_id', $this->countyItem);
		
		if ($iSortCol_0 == 0){
			$this->db->order_by('f.id '.$sSortDir_0);
		}else{
			$this->db->order_by('f.id desc');
		}
		$data = $this->mFeedback->pagination($this->mFeedback->tbFeedback.' f');
        foreach ($data['data'] as $k => $v){
            $data['aaData'][$k][] = dateFormat($v['addtime'], true);
            $data['aaData'][$k][] = $v['name'];
            $data['aaData'][$k][] = '<textarea readonly class="form-control remaks-item sss textarea_height">'.$v['content'].'</textarea>';
            $data['aaData'][$k][] = $v['reply'];
            $data['aaData'][$k][] = $this->mFeedback->status($v['status'], true);
            $btnConfig = '';
            if($v['status'] == 1) {
                $btnConfig = adminCreateBtn(array(
                    'label' => ' 回复',
                    'icon' => '',
                    'attr' => array(
                        'href' => 'javascript:;',
                        'class' => 'edit-btn btn default btn-xs blue-stripe',
                        'data-id' => $v['id'],
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'deliveryman',
                        'key' => 'feedback-reply'
                    )
                ));
            }
            $btnConfig .= ' '.adminCreateBtn(array(
                'label' => ' 删除',
                'icon' => '',
                'attr' => array(
                    'href' => getUrl('deliveryman/feedback/delete?id='.$v['id']),
                    'class' => 'ajax-request-btn btn default btn-xs red-stripe',
                    'alert-message' => "确认要删除吗？"
                ),
                'tag' => 'a',
                'permissions' => array(
                    'module' => 'deliveryman',
                    'key' => 'feedback-delete'
                )
            ));
            $data['aaData'][$k][] = $btnConfig;
        }
        unset($data['data']);
        echo json_encode($data);
	}

    /**
     * 回复
     */
    public function reply(){
        //表单验证
        $vConf = array(
            array('id', '反馈id', 'required|is_natural'),
            array('reply', '回复内容', 'required'),
        );
        $this->formDataValidation($vConf);
        $id = (int)post('id');
        $reply = trim(post('reply'));
        $data = [
        		 'status' => 2,
        		 'reply' => $reply
        ];
        
        $this->db->join($this->mMUser->tbUser.' u', 'u.id = f.deliveryman_id')
				 ->where_in('u.county_id', $this->countyItem)
				 ->where('f.id', $id);
        $tI = $this->mFeedback->count($this->mFeedback->tbFeedback.' f');
        if( ! $tI){
        	$this->ajaxReturn(0, '反馈信息不存在');
        }
        
        $this->db->where('id', $id);
        if($this->mFeedback->update($this->mFeedback->tbFeedback, $data)){
            $this->ajaxReturn(1, '已回复');
        }
        $this->ajaxReturn(0, '回复失败');
    }

    /**
     * 删除
     */
    public function delete()
    {
        //表单验证
        $vConf = array(
            array('id', '反馈id', 'required|is_natural'),
        );
        $this->formDataValidation($vConf, array('id' => get('id')));
        $id = (int)get('id');
        
        $this->db->join($this->mMUser->tbUser.' u', 'u.id = f.deliveryman_id')
			     ->where_in('u.county_id', $this->countyItem)
			     ->where('f.id', $id);
        $tI = $this->mFeedback->count($this->mFeedback->tbFeedback.' f');
        if( ! $tI){
        	$this->ajaxReturn(0, '反馈信息不存在');
        }
        
        $this->db->where('id', $id);
        $tId=$this->mFeedback->delete($this->mFeedback->tbFeedback);
        if(! $tId){
            $this->ajaxReturn(0,'删除失败');
        }
        $this->ajaxReturn(1,'删除成功');
    }
}
